//
//  OneKeyRepaymentVC.m
//  MRBang
//
//  Created by 铅笔 on 16/4/20.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "OneKeyRepaymentVC.h"

#import "AppDelegate.h"

@interface OneKeyRepaymentVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{

    UIButton *button_withdraw;
}


@property (nonatomic,strong) UITableView *tableView_OneKeyRepayment;

@property (nonatomic,strong) UITextField *textField_payMoney;
@property (nonatomic,strong) UITextField *textField_password;

@end

@implementation OneKeyRepaymentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"还款申请记录";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    [self initWithRewaradTskVCSubviews];
    
    //注册键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark --- 创建视图界面
- (void) initWithRewaradTskVCSubviews
{
    self.tableView_OneKeyRepayment = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-93*PROPORTION_WIDTH-20)];
    [self.view addSubview:self.tableView_OneKeyRepayment];
    self.tableView_OneKeyRepayment.dataSource = self;
    self.tableView_OneKeyRepayment.delegate = self;
    self.tableView_OneKeyRepayment.showsVerticalScrollIndicator = NO;
    self.tableView_OneKeyRepayment.separatorStyle =UITableViewCellSeparatorStyleNone;
    
    self.tableView_OneKeyRepayment.scrollEnabled = NO;
    self.tableView_OneKeyRepayment.backgroundColor = COLOR_EVERYVIEWBG;
    
    CGFloat buttonW = (kScreenWidth - (33*2+24)*PROPORTION_WIDTH)/2;
    
    //添加手势
    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTableViewTouchInSide)];
    [self.tableView_OneKeyRepayment addGestureRecognizer:tableViewGesture];
    
    //button  recharge
    UIButton *button_recharge = [[UIButton alloc] initWithFrame:CGRectMake(33*PROPORTION_WIDTH, kScreenHeight-95*PROPORTION_WIDTH-10, buttonW, 95*PROPORTION_WIDTH)];
    [self.view addSubview:button_recharge];
    [button_recharge setImage:[UIImage imageNamed:@"button_surePayMoney"] forState:UIControlStateNormal];
    button_recharge.tag = 21;
    [button_recharge addTarget:self action:@selector(rechargeAndWithdrawsAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //button  withdraw
    button_withdraw = [[UIButton alloc] initWithFrame:CGRectMake(button_recharge.right+24*PROPORTION_WIDTH,button_recharge.y, buttonW, button_recharge.height)];
    [self.view addSubview:button_withdraw];
    [button_withdraw setImage:[UIImage imageNamed:@"button_raplayMoney"] forState:UIControlStateNormal];
    button_withdraw.tag = 22;
    [button_withdraw addTarget:self action:@selector(rechargeAndWithdrawsAction:) forControlEvents:UIControlEventTouchUpInside];
}

//确认还款 申请帮我还 action
- (void) rechargeAndWithdrawsAction:(UIButton *)sender
{
    if (sender.tag==21) {
        //确认还款
       
        
    }
    if (sender.tag==22) {
        //申请帮我还
               
    }
    
}

#pragma  mark ----UITableViewDataSource

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
//    return 9;
    return 2;

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellFillInInformationID = @"cellFillInInformationID";
    
    UITableViewCell *cell_fillInfo = [tableView cellForRowAtIndexPath:indexPath];
    if (cell_fillInfo==nil) {
        cell_fillInfo = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellFillInInformationID];
    }
    
    UIView *view_line = [[UIView alloc] init];
    [cell_fillInfo.contentView addSubview:view_line];
    view_line.backgroundColor = [BaseViewController colorWithHexString:@"#EEEFF1"];
    
    cell_fillInfo.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSArray *array_title = @[@"贷款总额",@"逾期次数",@"本期共需还款金额",@"贷款总额",@"最近应还款时间",@"本期最低还款金额",@"账户余额",@"还款金额",@"交易密码"];
    
    //label left
    UILabel *label_leftTitle = [[UILabel alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, 35*PROPORTION_WIDTH, 210*PROPORTION_WIDTH, 32*PROPORTION_WIDTH)];
    [cell_fillInfo addSubview:label_leftTitle];
    label_leftTitle.text = array_title[indexPath.row];
    label_leftTitle.font = [UIFont systemFontOfSize:24*PROPORTION_WIDTH];
    
    //右侧 文字
    UILabel *label_rightContent = [[UILabel alloc] initWithFrame:CGRectMake(label_leftTitle.right+150*PROPORTION_WIDTH, label_leftTitle.y,330*PROPORTION_WIDTH, 34*PROPORTION_WIDTH)];
    [cell_fillInfo addSubview:label_rightContent];
    label_rightContent.font = [UIFont systemFontOfSize:Font_SIZE_30];
    label_rightContent.textColor = [BaseViewController colorWithHexString:@"#e8482e"];
    label_rightContent.text = @"12000.00元";
    
    //label left bottom
    UILabel *label_leftTitleBottom = [[UILabel alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, 70*PROPORTION_WIDTH, 210*PROPORTION_WIDTH, 32*PROPORTION_WIDTH)];
    [cell_fillInfo addSubview:label_leftTitleBottom];
    label_leftTitleBottom.font = [UIFont systemFontOfSize:24*PROPORTION_WIDTH];
    
    //右侧 文字 bottom
    UILabel *label_rightContentBottom = [[UILabel alloc] initWithFrame:CGRectMake(label_leftTitle.right+150*PROPORTION_WIDTH, label_leftTitleBottom.y,330*PROPORTION_WIDTH, 34*PROPORTION_WIDTH)];
    [cell_fillInfo addSubview:label_rightContentBottom];
    label_rightContentBottom.font = [UIFont systemFontOfSize:Font_SIZE_30];

    NSArray *array_right = @[@"已还额",@"逾期金额"];
    if (indexPath.row==0||indexPath.row==1) {
        //left
       label_leftTitle.textColor = [BaseViewController colorWithHexString:@"#989898"];
        //right
       label_rightContent.text = array_right[indexPath.row];
       label_rightContent.textColor = [BaseViewController colorWithHexString:@"#989898"];
       label_rightContent.font = [UIFont systemFontOfSize:Font_SIZE_24];
        
        if (indexPath.row==0) {
            //left bottom
            label_leftTitleBottom.text = @"0.00元";
            //right bottom
            label_rightContentBottom.text = @"0.00元";
        }
        else
        {
            //left bottom
            label_leftTitleBottom.text = @"0 次";
            //right bottom
            label_rightContentBottom.text = @"0.00元";
        }
    }
    else
    {
        view_line.frame = CGRectMake(0, 95*PROPORTION_WIDTH-1, kScreenWidth, 1.0);
        label_leftTitle.textColor = [BaseViewController colorWithHexString:@"#333333"];
    }
    label_leftTitle.text = array_title[indexPath.row];
    
    if (indexPath.row==0) {
        view_line.frame = CGRectMake(0, 124*PROPORTION_WIDTH-1, kScreenWidth, 1.0);

    }
    if (indexPath.row==1) {
        view_line.frame = CGRectMake(0, (130-12)*PROPORTION_WIDTH, kScreenWidth, 12*PROPORTION_WIDTH);
    }
    
#warning 这里面是 如果没有 贷款总额 则不展示这些信息  目前处理是全部屏蔽掉
//    if (indexPath.row==4) {
//        label_rightContent.textColor = [BaseViewController colorWithHexString:@"#989898"];
//        label_rightContent.text = @"2016-03-02";
//    }
//    if (indexPath.row==6) {
//       label_rightContent.text = @"5000.00元";
//        
//        //button
//        UIButton *button_helpMeGet = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-170*PROPORTION_WIDTH, 28*PROPORTION_WIDTH, 85*PROPORTION_WIDTH, 45*PROPORTION_WIDTH)];
//        [cell_fillInfo.contentView addSubview:button_helpMeGet];
//        [button_helpMeGet setImage:[UIImage imageNamed:@"button_get"] forState:UIControlStateNormal];
//        [button_helpMeGet addTarget:self action:@selector(rechargeAction) forControlEvents:UIControlEventTouchUpInside];
//        
//    }
//    if (indexPath.row==7) {
//        
//        label_rightContent.text = nil;
//        
//        //moeny
//        self.textField_payMoney = [[UITextField alloc] initWithFrame:CGRectMake(label_rightContent.x,label_rightContent.y,kScreenWidth - 100, label_rightContent.height)];
//        [cell_fillInfo.contentView addSubview:self.textField_payMoney];
//        self.textField_payMoney.delegate = self;
//        self.textField_payMoney.font = [UIFont systemFontOfSize:Font_SIZE_22];
//        self.textField_payMoney.tag = 1100;
//        self.textField_payMoney.placeholder = @"请输入还款金额";
//        self.textField_payMoney.keyboardType = UIKeyboardTypeNumberPad;
//    }
//    
//    if (indexPath.row==8) {
//        
//        label_rightContent.text = nil;
//
//        //pass word
//        self.textField_password = [[UITextField alloc] initWithFrame:CGRectMake(label_rightContent.x,label_rightContent.y,kScreenWidth - 100, label_rightContent.height)];
//        [cell_fillInfo.contentView addSubview:self.textField_password];
//        self.textField_password.delegate = self;
//        self.textField_password.tag = 1101;
//        self.textField_password.font = [UIFont systemFontOfSize:Font_SIZE_22];
//        self.textField_password.placeholder = @"请输入交易密码";
//        self.textField_password.returnKeyType = UIReturnKeyDone;
//    }
    return cell_fillInfo;
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
      return  124*PROPORTION_WIDTH;
    }
    if (indexPath.row==1) {
        return 130*PROPORTION_WIDTH;
    }
    else
    {
        return 95*PROPORTION_WIDTH;
    }
}

#pragma mark --- 充值
- (void) rechargeAction
{
    NSLog(@"充值");

}

#pragma mark  --- 点击tableView 收回键盘
- (void)commentTableViewTouchInSide{
    [self.textField_password resignFirstResponder];
    [self.textField_payMoney resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)aTextfield {
    [self.textField_password resignFirstResponder];//关闭键盘
    return YES;
}

#pragma mark keyboardNotification

- (void)keyboardWillShow:(NSNotification*)notification{
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //如果self在键盘之下 才做偏移
    if ([self convertYToWindow:CGRectGetMaxY(button_withdraw.frame)]>=_keyboardRect.origin.y)
    {
        //没有偏移 就说明键盘没出来，使用动画
        if (self.view.frame.origin.y== button_withdraw.frame.origin.y) {
            
            [UIView animateWithDuration:0.3 animations:^{
                self.view.frame = CGRectMake(0, -_keyboardRect.size.height+[self getHeighOfWindow]-CGRectGetMaxY(self.view.frame), kScreenWidth, kScreenHeight);
            }];
        }
        else
        {
            self.view.frame = CGRectMake(0, -150, kScreenWidth, kScreenHeight);
        }
    }
}

- (void)keyboardWillHide:(NSNotification*)notification{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
}

#pragma  mark ConvertPoint
//将坐标点y 在window和superview转化  方便和键盘的坐标比对
-(float)convertYFromWindow:(float)Y
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CGPoint o = [appDelegate.window convertPoint:CGPointMake(0, Y) toView:self.view];
    return o.y;
    
}
-(float)convertYToWindow:(float)Y
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CGPoint o = [self.view convertPoint:CGPointMake(0, Y) toView:appDelegate.window];
    return o.y;
    
}
-(float)getHeighOfWindow
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    return appDelegate.window.frame.size.height;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
